package br.unifor.lec.aco.util;

public class CalculaR2 {
	//Calcula média aritimetica
    public static double mediaAritmetica(double []array){
        return somatorio(array)/array.length;
    }
   
    //Calcula somatório
    public static double somatorio(double []array){
       
        double somatorio = 0;
        for (double valor : array) {
            somatorio += valor;
        }
        return somatorio;
    }
   
    //Calcula alpha
    public static double calcAlpha(double x[], double y[]){
        double alphaNum = 0.0;
        double alphaDen = 0.0;
        int i = 0;
       
        double mediaX = mediaAritmetica(x);
        double mediaY = mediaAritmetica(y);
       
        while (i < x.length) {
            alphaNum += (x[i]-mediaX) * (y[i] - mediaY);
            alphaDen += Math.pow((x[i] - mediaX), 2);
            i++;
        }
        if(alphaDen == 0)
            return 0.0;
        else
            return alphaNum/alphaDen;
    }
   
    public static double calcR2(double slope, double x[], double y[]){
        int i = 0;
        double sx = 0;
        double sy = 0;
       
        double mediaX = mediaAritmetica(x);
        double mediaY = mediaAritmetica(y);
       
        while (i < x.length){
            sx += Math.pow((x[i] - mediaX), 2);
            sy += Math.pow((y[i] - mediaY), 2);
            i++;
        }
       
        double ssX = Math.sqrt(sx / (x.length -1));
        double ssY = Math.sqrt(sy / (x.length -1));
       
        return (slope * ssX)/ssY;
    }
    
}
